const response = require('../../utils/response');
const auth = require('../../utils/auth');
const adminService = require('../services/adminService');
const { getUserById } = require('../services/userService');
class AuthController {
    async refreshToken(ctx){
        if(!ctx.state.admin){
            return response.error(ctx, "登录状态已过期", null, 401)
        }
        const admin = await adminService.getAdminById(ctx.state.admin.dataValues.id);
        response.success(ctx, auth.sing({
            data: {
                ...admin,
                roles: ['admin', 'editor'] // 先写死给前端
            },
            role: 'admin'
        }));
    }
    async refreshUserToken(ctx){
        if(!ctx.state.user){
            return response.error(ctx, "登录状态已过期", null, 401)
        }
        const row = await getUserById(ctx.state.user.id);

        const authData = auth.sing({
            data: row,
            role: 'user'
        })
        delete row.dataValues.password;
        response.success(ctx, {
            userInfo: row,
            ...authData
        });
    }
}

module.exports = new AuthController();